//
//  STDSConstrastEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/4/3.
//

#ifndef STDSConstrastEffect_h
#define STDSConstrastEffect_h


#include "STDSBaseEffect.hpp"

class STDSConstrastEffect: public STDSBaseEffect {
public:
    STDSConstrastEffect() {
        mEffectType = STDSEffectTypeContrast;
    }
    ~STDSConstrastEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
        STDSBaseEffect::initGL();
    }
    
    void setFloatArguments(const char *name, float *floatValue, int n) override {
        if (strcmp(name, "strength") == 0) {
            mStrength = *floatValue;
        } else if (strcmp(name, "radius") == 0) {
            mRadius = *floatValue;
        }
    }
    
public:
    shared_ptr<STDSShaderProgram> getEffectProgram() {
        if (mConstrastProgram == nullptr) {
            mConstrastProgram = mContext->getProgram(kShaderSRCContrast);
        }
        return mConstrastProgram;
    }
    
    
    void draw() override {
        auto program = getEffectProgram();
        if (program == nullptr) {
            return;
        }
        if (mTexture == nullptr) {
            return;
        }
        
        program->use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTexture->getId());
        program->setUniform(program->getUniform("inputImageTexture"), 0);
        program->setUniform(program->getUniform("strength"), mStrength);
        program->setUniform(program->getUniform("radius"), mRadius);
        program->setUniform2f(program->getUniform("size"), mTexture->getWidth(), mTexture->getHeight());
        
        mAtbRect.setPosVex(program->getAttrib("aPos"));
        mAtbRect.setPosTex(program->getAttrib("aTexCoord"));
        mAtbRect.draw();
        program->unuse();
    }

private:
    float mStrength{0.0};
    float mRadius{0.0};
    shared_ptr<STDSShaderProgram> mConstrastProgram{nullptr};
};
#endif /* STDSConstrastEffect_h */
